package models;

import java.util.Date;

import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import play.db.jpa.Model;

@MappedSuperclass
public class BaseEntity extends Model{
	
	public enum PeopleType {
		ADMIN, USER
	}
	
	//----------------创建者信息开始------------------//
	/*创建日期*/
	public Date createAt;
	
	/*创造人的类型(管理员、用户)*/
	public PeopleType creatorType;
	
	/*创建人*/
	@ManyToOne(fetch = FetchType.EAGER)
	public User createByUser;
	
	/*最后修改人*/
	@ManyToOne(fetch = FetchType.EAGER)
	public Administrator createByAdmin;
	//----------------创建者信息结束------------------//
	
	
	//----------------修改者信息开始------------------//
	/*最后修改日期*/
	public Date lastModifiedAt;
	
	/*最后修改人的类型(管理员、用户)*/
	public PeopleType modifierType;
	
	/*最后修改人*/
	@ManyToOne(fetch = FetchType.EAGER)
	public User lastModifiedByUser;
	
	/*最后修改人*/
	@ManyToOne(fetch = FetchType.EAGER)
	public Administrator lastModifiedByAdmin;
	//----------------修改者信息结束------------------//
	
	
	/*记录的状态，是否可用，用于软删除*/
	public Status status = Status.ENABLE;
	
	public enum Status {
		DISABLED, ENABLE
	}

	@PrePersist
	protected void onCreate() {
		lastModifiedAt = createAt = new Date();
	}

	@PreUpdate
	protected void onUpdate() {
		lastModifiedAt = new Date();
	}
	
	public void setCreator(User user) {
		this.createAt = new Date();
		this.createByUser = user;
		this.creatorType = PeopleType.USER;
	}
	
	public void setCreator(Administrator admin) {
		this.createAt = new Date();
		this.createByAdmin = admin;
		this.creatorType = PeopleType.ADMIN;
	}
	
	public void resetModifier(User user) {
		this.lastModifiedAt = new Date();
		this.lastModifiedByUser = user;
		this.modifierType = PeopleType.USER;
	}
	
	public void resetModifier(Administrator admin) {
		this.lastModifiedAt = new Date();
		this.lastModifiedByAdmin = admin;
		this.modifierType = PeopleType.ADMIN;
	}
}
